<?php
class User_Controller extends Base_Controller
{    

    public $restful = true;


    public function get_userList()
    {
        $data['userlist'] = $allUser = User::listUser();

        return View::make('user.userlist',$data);
    }

    public function post_addUser()
    {
        $input = Input::all();

        return var_dump($input);
    }

    public function get_userInfo()
    {
        

    }


    public function get_profile()
    {

        $logged_user = Auth::user();

        $data['userInfo'] = User::find($logged_user->userid)->userprofile;

        return View::make('user.profile',$data);
    }


    public function post_profile()
    {

        $input = Input::all();

        $validation = UserProfile::validate($input);

        if( $validation->fails() ) {
            return Redirect::to('user/profile')->with_errors($validation)->with_input();
        }

        $extension = File::extension($input['photo']['name']);
        $directory = path('public').'avatar/'.sha1(Auth::user()->userid);
        $filename = sha1(Auth::user()->userid.time()).".{$extension}";


        $profile = UserProfile::find(Auth::user()->userid);

        if($input['photo']['size'] != null){
            $upload_success = Input::upload('photo', $directory, $filename);

            if( $upload_success ) {

                while(is_file('public/'.$profile->imgpath) == TRUE)
                {
                    chmod('public/'.$profile->imgpath, 0666);
                    unlink('public/'.$profile->imgpath);
                }

                $profile->imgpath = 'avatar/'.sha1(Auth::user()->userid).'/'.$filename;
            }
        }

        $profile->fullname  = $input['fullname'];
        $profile->dob  = date('Y-m-d',strtotime($input['dob']));
        $profile->address = $input['address'];
        $profile->postcode = $input['postcode'];

        $profile->save();

        return Redirect::to('user/profile');

    }

}